package com.example.userservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.commonservice.Common.Result;
import com.example.userservice.entity.User;
import com.example.userservice.entity.VolunteerProfile;
import com.example.userservice.service.UserService;
import com.example.userservice.service.VolunteerProfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    // 用户管理接口
    @PostMapping("/add")
    public Result<String> addUser(@RequestBody User user) {
        user.setCreateTime(LocalDateTime.now());
        userService.save(user);
        return Result.success("用户新增成功，ID：" + user.getId());
    }

    @PostMapping("/delete")
    public Result<String> deleteUser(@RequestParam Long id) {
        boolean success = userService.removeById(id);
        return success ? Result.success("用户删除成功") : Result.error("用户不存在，删除失败");
    }

    @PostMapping("/update")
    public Result<String> updateUser(@RequestBody User user) {
        boolean success = userService.updateById(user);
        return success ? Result.success("用户更新成功") : Result.error("用户不存在，更新失败");
    }
    //http://localhost:8000/user-service/user/list
    @GetMapping("/list")
    public Result<IPage<User>> listUsers(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) Integer role) {

        Map<String, Object> params = new HashMap<>();
        params.put("username", username);
        params.put("role", role);
        return Result.success(userService.pageUsers(current, size, params));
    }


}
